<?php
/**
 * 礼物
 * @date    2020-01-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */
namespace App\Http\Controllers\Api\V1;

use App\Models\Config;
use App\Models\Gift;
use App\Models\Give;
use App\Models\UserGiftLog;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GiftController extends ApiController
{
    // 列表
    public function list(Request $request)
    {
        $page = (int)$request->get('page', 1);
        $limit = (int)$request->get('limit', 10);
        $offset = ($page - 1) * $limit;
        $total = Gift::where('status', 1)
            ->where(function ($query) use ($request) {
                $request->type and $query->where('type', $request->type);
            })->count();
        $data = Gift::where('status', 1)
            ->where(function ($query) use ($request) {
                $request->type and $query->where('type', $request->type);
            })
            ->offset($offset)
            ->limit($limit)
            ->orderBy('sort', 'ASC')
            ->orderBy('id', 'ASC')
            ->get();
        foreach ($data as &$value) {
            $value['url'] = dealUrl($value['url']);
            $value['video_url'] = dealUrl($value['video_url']);
        }
        return $this->success('成功', [
            'total' => $total,
            'total_page' => ceil($total / $limit),
            'current_page' => $page,
            'list' => $data
        ]);
    }

    /**
     * 送礼物
     * @param Request $request
     */
    public function give(Request $request)
    {
        $to_user_id = $request->get('to_user_id');
        $gift_id = $request->get('gift_id', 0);
        $type = $request->get('type') ?:'AMOUNT';
        if (empty($this->getUserId())) {
                return $this->error('请先登录');
        }
        if (empty($to_user_id)) {
            return $this->error('送礼物user_id不能为空');
        }
        if (empty($gift_id)) {
            return $this->error('礼物ID不能为空');
        }
        if ($to_user_id == $this->getUserId()) {
            return $this->error('自己不能给自己送礼物');
        }

        $gift = Gift::where('status', 1)->where('id', $gift_id)->first();

        if (empty($gift)) {
            return $this->error('礼物不存在');
        }

        if (empty($gift->price)) {
            return $this->error('礼物金额有问题');
        }

        $config = Config::getConfig();

        $base_fee_rate = $config['base_fee_rate'] ??0;

        DB::beginTransaction();
        try {

            $res = UserGiftLog::add($this->getUserId(), $to_user_id, $gift_id, $gift->price, $base_fee_rate);
            if (!$res) {
                DB::rollBack();
                return $this->error('送礼失败');
            }

            $get_amount = round($gift->price * (100 - $base_fee_rate) / 100, 2);

            UserService::doAccount($this->getUserId(), -$gift->price, '送礼扣款', $type, 'GIFT_GIVE');
            UserService::doAccount($to_user_id, $get_amount, '送礼扣款', $type, 'GIFT_GIVE');
            DB::commit();
            return $this->success('送礼成功');
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->error($e->getMessage());
        }
    }

    // 我的送礼列表
    public function me(Request $request)
    {
        $user_id = (int)$request->get('user_id', $this->getUserId());
        $page = (int)$request->get('page', 1);
        $limit = (int)$request->get('limit', 10);
        $offset = ($page - 1) * $limit;

        if (empty($user_id)) {
            return $this->error('user_id不能为空');
        }

        $total = UserGiftLog::where('status', 1)
            ->where('user_id', $user_id)
            ->count();
        $data = UserGiftLog::where('status', 1)
            ->where('user_id', $user_id)
            ->offset($offset)
            ->limit($limit)
            ->orderBy('id', 'DESC')
            ->get();
        foreach ($data as &$value) {
            $gift = Gift::where('id', $value['gift_id'])->first();
            $gift->url = dealUrl($gift->url);
            $gift->video_url = dealUrl($gift->video_url);
            $value['gift'] = $gift;
        }
        return $this->success('成功', [
            'total' => $total,
            'total_page' => ceil($total / $limit),
            'current_page' => $page,
            'list' => $data
        ]);
    }

    // 我收到礼物列表
    public function receive(Request $request)
    {
        $user_id = (int)$request->get('user_id', $this->getUserId());
        $process = (int)$request->get('process', 0);
        $page = (int)$request->get('page', 1);
        $limit = (int)$request->get('limit', 10);
        $offset = ($page - 1) * $limit;

        if (empty($user_id)) {
            return $this->error('user_id不能为空');
        }

        $total = UserGiftLog::where('status', 1)
            ->where('to_user_id', $user_id)
            ->where(function($query) use ($process){
                if ($process) {
                    $query->where('created_at', '>' , strtotime("-5 minute"));
                    $query->where('is_read', 0);
                }
            })
            ->count();
        $data = UserGiftLog::where('status', 1)
            ->where('to_user_id', $user_id)
            ->where(function($query) use ($process){
                if ($process) {
                    $query->where('created_at', '>' , strtotime("-5 minute"));
                    $query->where('is_read', 0);
                }
            })
            ->offset($offset)
            ->limit($limit)
            ->orderBy('id', 'ASC')
            ->get();
        foreach ($data as &$value) {
            $gift = Gift::where('id', $value['gift_id'])->first();
            $gift->url = dealUrl($gift->url);
            $gift->video_url = dealUrl($gift->video_url);
            $value['gift'] = $gift;
            if ($process) {
                UserGiftLog::where('id', $value['id'])->update(['is_read' => 1]);
            }
        }
        return $this->success('成功', [
            'total' => $total,
            'total_page' => ceil($total / $limit),
            'current_page' => $page,
            'list' => $data
        ]);
    }
}
